package com.geovis.emergency.spd.biz.filing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.IterUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.geovis.common.mybatis.page.PageParam;
import com.geovis.common.mybatis.page.PageResult;
import com.geovis.emergency.spd.biz.filing.mapper.FilingSystemFileBusinessMapper;
import com.geovis.emergency.spd.biz.filing.mapper.TbPlanFilingsMapper;
import com.geovis.emergency.spd.biz.filing.service.IFilingSystemDeptService;
import com.geovis.emergency.spd.biz.filing.service.IFilingSystemFileService;
import com.geovis.emergency.spd.biz.filing.service.ITbPlanFilingsService;
import com.geovis.emergency.spd.cache.DictCache;
import com.geovis.emergency.spd.entity.filing.entity.FilingSystemDept;
import com.geovis.emergency.spd.entity.filing.entity.FilingSystemFile;
import com.geovis.emergency.spd.entity.filing.entity.FilingSystemFileBusiness;
import com.geovis.emergency.spd.entity.filing.entity.TbPlanFilings;
import com.geovis.emergency.spd.entity.filing.pojo.GroupStatTypeEnum;
import com.geovis.emergency.spd.entity.pc.pojo.constant.PcDisasterConstant;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcPlanDetailVO;
import com.geovis.emergency.spd.entity.pc.pojo.vo.PcPlanGroupByVO;
import com.geovis.emergency.spd.entity.system.constant.SystemFileBusinessConstant;
import com.geovis.emergency.spd.entity.system.entity.SystemDept;
import com.geovis.emergency.spd.entity.system.entity.SystemDict;
import com.geovis.emergency.spd.entity.system.entity.SystemFile;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 曾德实
 * @since 2023-08-14
 */

@Service
@RequiredArgsConstructor
@Slf4j
@DS("filing")
public class TbPlanFilingsServiceImpl extends ServiceImpl<TbPlanFilingsMapper, TbPlanFilings> implements ITbPlanFilingsService {


    private final FilingSystemFileBusinessMapper filingSystemFileBusinessMapper;
//
//
    private final IFilingSystemFileService filingSystemFileService;

    private final IFilingSystemDeptService filingSystemDeptService;

    @Override
    public List<TbPlanFilings> getList() {
        return this.list(Wrappers.lambdaUpdate(TbPlanFilings.class));
    }

    @Override
    public PcPlanDetailVO getDetail(String filingPlanId) {
        PcPlanDetailVO planDetailVO = new PcPlanDetailVO();
        TbPlanFilings planFilings = this.getById(filingPlanId);
        if (planFilings == null)
        {
            return planDetailVO;
        }
        planDetailVO.setTitle(planFilings.getPlanName());
        planDetailVO.setType(planFilings.getEventType());

        //附件
        planDetailVO.setPlaningFileList(getFileList(filingPlanId, SystemFileBusinessConstant.FILE_EMERGENCY));
        planDetailVO.setOperationManualFileList(getFileList(filingPlanId,SystemFileBusinessConstant.FILE_OPT));
        planDetailVO.setOverViewFileList(getFileList(filingPlanId,SystemFileBusinessConstant.FILE_FILINGS));
        planDetailVO.setOtherFileList(getFileList(filingPlanId,SystemFileBusinessConstant.FILE_OTHER));
        return planDetailVO;
    }

    @Override
    public List<PcPlanGroupByVO> groupBy(GroupStatTypeEnum type) {
        List<TbPlanFilings> planFilings=this.list();
        Map<String,List<TbPlanFilings>> planFilingMap=planFilings.stream().collect(Collectors.groupingBy(type.getFunction()));


        return planFilingMap.keySet().stream().map(groupType->{
            PcPlanGroupByVO groupByVO=new PcPlanGroupByVO();
            //分级
            if(type.equals(GroupStatTypeEnum.LEVEL))
            {
                if("1".equals(groupType))
                {
                    groupByVO.setTitle("省级");
                }
                if("2".equals(groupType))
                {
                    groupByVO.setTitle("市级");
                }
                if("3".equals(groupType))
                {
                    groupByVO.setTitle("区级");
                }
            }
            //部门
            if(type.equals(GroupStatTypeEnum.DEPT))
            {
                FilingSystemDept dept=filingSystemDeptService.getById(groupType);
                groupByVO.setTitle(dept.getDeptName());
            }
            if(type.equals(GroupStatTypeEnum.TYPE))
            {
                String name=DictCache.getDictValue(PcDisasterConstant.DISASTER_TYPE, groupType);
                groupByVO.setTitle(name);
            }
            groupByVO.setCount(planFilingMap.get(groupType).size());
            return groupByVO;
        }).collect(Collectors.toList());
    }

    private List<SystemFile> getFileList(String relatedId, String type){
        //获取附件信息
        List<FilingSystemFileBusiness> fileBusiness = filingSystemFileBusinessMapper.selectList(Wrappers.lambdaQuery(FilingSystemFileBusiness.class)
            .eq(FilingSystemFileBusiness::getParam1,relatedId)
            .eq(FilingSystemFileBusiness::getParam2,type)
            .orderByDesc(FilingSystemFileBusiness::getCreateTime));

        return fileBusiness.stream().map(fb->{
            FilingSystemFile filingSystemFile=filingSystemFileService.getById(fb.getFileId());
            //将预案备案文件信息，插入到预案这边
            SystemFile file=new SystemFile();
            BeanUtils.copyProperties(filingSystemFile,file);
            return file;

        }).collect(Collectors.toList());
    }


}
